﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class FilmController : Controller
    {
        private FilmBLO _boFilm = new FilmBLO();
        private CategoryBLO _bloCate = new CategoryBLO();
        private FormatBLO _bloFormat = new FormatBLO();

        /// <summary>
        /// Redirect to Index page. Load all film into list
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View(_boFilm.LoadFilms());
        }


        public ActionResult Details(int id = 0)
        {
            Film film = _boFilm.LoadFilmsById(id);
            if (film == null)
            {
                return HttpNotFound();
            }
            ViewBag.Film = film;
            int cateId = int.Parse(film.CategoryId.ToString().Trim());
            String cateName = _bloCate.LoadCategoryById(cateId).CategoryName;
            ViewBag.CatName = cateName;
            int formatId = int.Parse(film.FormatId.ToString().Trim());
            String formatName = _bloFormat.LoadFormatsById(formatId).FormatName;
            ViewBag.FortName = formatName;
            return View(film);
        }


        /// <summary>
        /// Create a film
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(_bloCate.LoadCategories(), "CategoryId", "CategoryName");
            ViewBag.FormatId = new SelectList(_bloFormat.LoadFormats(), "FormatId", "FormatName");
            return View();
        }

        /// <summary>
        /// Show partial view for CategoryPartial, is used when user want add new Category from CreateFilm form
        /// </summary>
        /// <param name="categoryModel"></param>
        /// <returns>Show partial view Category</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/03</date>
        public PartialViewResult CategoryPartial(Category categoryModel)
        {
            return PartialView("CategoryPartial", categoryModel);
        }

        /// <summary>
        /// Show partial view for FormatPartial, is used when user want add new Format from CreateFilm form
        /// </summary>
        /// <param name="categoryModel"></param>
        /// <returns>Show partial view Format</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/03</date>
        public PartialViewResult FormatPartial(Format formatModel)
        {
            return PartialView("FormatPartial", formatModel);
        }

        /// <summary>
        /// Create a new Category from CreateFilm
        /// </summary>
        /// <param name="categoryModel"></param>
        /// <returns>Redirect to CreateFilm page after create new Category</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/03</date>
        [HttpPost]
        public PartialViewResult CreateCategoryPartial(Category categoryModel)
        {
            if (ModelState.IsValid)
            {
                _bloCate.CreateCategory(categoryModel);
            }

            ViewBag.CategoryId = new SelectList(_bloCate.LoadCategories(), "CategoryId", "CategoryName");
            ViewBag.FormatId = new SelectList(_bloFormat.LoadFormats(), "FormatId", "FormatName");
            return PartialView("Create");
        }

        /// <summary>
        /// Create a new Format from CreateFilm
        /// </summary>
        /// <param name="categoryModel"></param>
        /// <returns>Redirect to CreateFilm page after create new Format</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/03</date>
        [HttpPost]
        public PartialViewResult CreateFormatPartial(Format formatModel)
        {
            if (ModelState.IsValid)
            {
                _bloFormat.CreateFormat(formatModel);
            }

            ViewBag.CategoryId = new SelectList(_bloCate.LoadCategories(), "CategoryId", "CategoryName");
            ViewBag.FormatId = new SelectList(_bloFormat.LoadFormats(), "FormatId", "FormatName");
            return PartialView("Create");
        }

        /// <summary>
        /// Create a film. Save film and image of film
        /// </summary>
        /// <param name="film"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Film film, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Image/FilmImage/") + file.FileName);
                    film.ImageURL = file.FileName;
                }

                _boFilm.CreateFilm(film);

                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(_bloCate.LoadCategories(), "CategoryId", "CategoryName", film.CategoryId);
            ViewBag.FormatId = new SelectList(_bloFormat.LoadFormats(), "FormatId", "FormatName", film.FormatId);
            return View(film);
        }

        public ActionResult Edit(int id = 0)
        {
            Film film = _boFilm.LoadFilmsById(id);
            if (film == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(_bloCate.LoadCategories(), "CategoryId", "CategoryName");
            ViewBag.FormatId = new SelectList(_bloFormat.LoadFormats(), "FormatId", "FormatName");
            return View(film);
        }

        [HttpPost]
        public ActionResult Edit(Film film, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Image/FilmImage/") + file.FileName);
                    film.ImageURL = file.FileName;
                }

                _boFilm.EditFilm(film);
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(_bloCate.LoadCategories(), "CategoryId", "CategoryName");
            ViewBag.FormatId = new SelectList(_bloFormat.LoadFormats(), "FormatId", "FormatName");
            return View(film);
        }

        /// <summary>
        /// Delete a film based on FilmId
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Redirect to Index page</returns>
        /// <author>nguyen</author>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            Film film = _boFilm.LoadFilmsById(id);
            if (film == null)
            {
                return HttpNotFound();
            }
            _boFilm.DeleteFilm(film);
            return RedirectToAction("Index");
        }

    }
}
